{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0a3dac8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 4.1 ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e1e239d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#在终端/命令行提示符中，依次输入如下指令，配置用于运行本章代码的编程环境。\n",
    "\n",
    "#1) 检查终端的虚拟环境python_pandas：conda env list\n",
    "\n",
    "#2) 切换到虚拟环境python_pandas：conda activate python_xst\n",
    "\n",
    "#3) 启动Jupyter Notebook：jupyter notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "588701be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting package metadata (current_repodata.json): done\n",
      "Solving environment: done\n",
      "\n",
      "# All requested packages already installed.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!conda install pandas   ##查验当前虚拟环境是否安装了pandas 并更新到最新版"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "52006c3a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.4.2'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas\n",
    "pandas.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4df2a3c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting package metadata (current_repodata.json): done\n",
      "Solving environment: done\n",
      "\n",
      "## Package Plan ##\n",
      "\n",
      "  environment location: /Users/michael_fan/opt/anaconda3/envs/python_xst\n",
      "\n",
      "  added / updated specs:\n",
      "    - openpyxl\n",
      "\n",
      "\n",
      "The following NEW packages will be INSTALLED:\n",
      "\n",
      "  et_xmlfile         pkgs/main/osx-64::et_xmlfile-1.1.0-py38hecd8cb5_0\n",
      "  openpyxl           pkgs/main/noarch::openpyxl-3.0.9-pyhd3eb1b0_0\n",
      "\n",
      "\n",
      "Preparing transaction: done\n",
      "Verifying transaction: done\n",
      "Executing transaction: done\n"
     ]
    }
   ],
   "source": [
    "##!!这里是不是要补充如果没有安装的安装方法###\n",
    "!conda install openpyxl -y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "96484687",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openpyxl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6069da48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.0.9'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openpyxl.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "6e0e58fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 4.2 ###\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "cc1c4701",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c2107fd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#自定义索引\n",
    "s1 = pd.Series(data = [1, 2, 3])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "dbab5a84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a     1.0\n",
       "1    10.0\n",
       "2    10.3\n",
       "dtype: float64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建序列\n",
    "D1 = {\"a\":1, 1:10, 2:10.3}\n",
    "s2 = pd.Series(D1)\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "09e0c59e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nation        [China, America, UK]\n",
       "population            [13, 3, 0.6]\n",
       "dtype: object"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建复杂序列\n",
    "D3 = {'nation':['China','America','UK'] , \n",
    "     \"population\":[13, 3,0.6]}\n",
    "s3 = pd.Series(D3)\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3fe8593c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nation</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>China</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>America</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>UK</td>\n",
       "      <td>0.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    nation  population\n",
       "0    China        13.0\n",
       "1  America         3.0\n",
       "2       UK         0.6"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#创建数据框\n",
    "DF3 = pd.DataFrame(D3)\n",
    "DF3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "6d9997b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nation</th>\n",
       "      <th>population</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>China</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>America</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>UK</td>\n",
       "      <td>0.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    nation  population\n",
       "1    China        13.0\n",
       "2  America         3.0\n",
       "3       UK         0.6"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#自定义索引的数据框\n",
    "DF4 = pd.DataFrame(D3, index=[1,2,3])\n",
    "DF4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "592ce0a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>one</th>\n",
       "      <th>two</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>c</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>NaN</td>\n",
       "      <td>d</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   one two\n",
       "1    a   a\n",
       "2    b   b\n",
       "3    c   c\n",
       "4  NaN   d"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用多个不同索引的Pandas序列，创建一个Pandas数据框。\n",
    "d = {'one': pd.Series(['a', 'b', 'c'], index=[1, 2, 3]),\n",
    "     'two': pd.Series(['a', 'b', 'c', 'd'], index=[1, 2, 3, 4])}\n",
    "\n",
    "df = pd.DataFrame(d)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9405a799",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
